package com.sscl.easyble.receiver

import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.sscl.easyble.Logger
import com.sscl.easyble.variable.GlobalVariable

/**
 * 蓝牙配对广播接收器
 */
internal class DeviceBoundBroadcastReceiver : BroadcastReceiver() {

    companion object {
        fun getIntentFilter(): IntentFilter {
            val  intentFilter = IntentFilter()
            intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
            intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST)
            return intentFilter
        }

        private const val TAG = "DeviceBoundBroadcastReceiver"
    }

    init {
        Logger.log(TAG, "蓝牙配对广播接收器 init")
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 实现方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    override fun onReceive(context: Context, intent: Intent) {
        val action: String = intent.action ?: return
        when (action) {
            BluetoothDevice.ACTION_PAIRING_REQUEST -> {
                Logger.log(TAG, "正在请求配对")
                val mType: Int =
                    intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR)
                Logger.log(TAG, "mType = $mType")
                when (mType) {
                    BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION -> Logger.log(
                        TAG,
                        "需要让用户确认配对PIN码"
                    )

                    else -> {
                        Logger.log(
                            TAG,
                            "mType: $mType,暂未处理"
                        )
                    }
                }
            }

            BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {
                Logger.log(TAG, "设备绑定状态变更")
                val bondState: Int =
                    intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR)
                val device = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    intent.getParcelableExtra(
                        BluetoothDevice.EXTRA_DEVICE,
                        BluetoothDevice::class.java
                    ) ?:return
                } else {
                    @Suppress("DEPRECATION")
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice? ?:return
                }
                Logger.log(TAG, "bondState = $bondState device = $device")
                when (bondState) {
                    BluetoothDevice.BOND_BONDING -> {
                        Logger.log(TAG, "设备正在绑定")
                        GlobalVariable.onDeviceBondStateChangedListeners[device.address]?.onDeviceBinding()
                    }

                    BluetoothDevice.BOND_BONDED -> {
                        Logger.log(TAG, "设备已经绑定")
                        GlobalVariable.onDeviceBondStateChangedListeners[device.address]?.onDeviceBonded()
                    }

                    BluetoothDevice.BOND_NONE -> {
                        Logger.log(TAG, "设备绑定失败（或设备取消绑定）")
                        GlobalVariable.onDeviceBondStateChangedListeners[device.address]?.onDeviceBindNone()
                    }

                    else -> {
                        Logger.log(TAG, "其他设备绑定状态 $bondState ，未处理")
                        GlobalVariable.onDeviceBondStateChangedListeners[device.address]?.unknownBoundState(bondState)
                    }
                }
            }

            else -> {
                Logger.log(
                    TAG,
                    "action: $action,暂未处理"
                )
            }
        }
    }
}